const app = getApp();
import {
  getCitysList,
  getWxUser,
  getOpenId,
  getOneId,
  AdvertisingList,
  goodsList,
  assignDetail,
  giftInfo,
  getOneDept
} from './../../../server/api'
Page({
  data: {
    oss_url: app.globalData.oss_url,
    openId: app.globalData.user_info.openId,
    phone: app.globalData.user_info.phone,
    city: app.globalData.city,
    currentIndex: 0,
    show: true,
    currentCity: '',
    pageNum:1,
    pageSize:10,
    navigation_show: true,
    adlist:[],
    oneId:'',
    active:0,
    haveORno:0,
    giftId:4,
    goodsList:[],
    giftMsg:{},    //所属部门id拿取的礼金包信息
    giftIdMsg:{},   //礼金包id拿取的信息
    dealersInfo:{}   //经销商信息
  },
  onLoad: function (options) {
  },
  onShow: function () {
    //解决自定义tabbar中 地址切换正确，但选中样式不生效的问题
    if(typeof this.getTabBar=== 'function' && this.getTabBar()){
      this.getTabBar().setData({
        active:0
      })
    }
    this.setData({
      currentCity:app.globalData.city
    })
    this.get_openId();
    this.getAd()
    this.cityData()
    this.getAssignDetail()
  },
  getAd(){
    AdvertisingList({pageNum:1,pageSize:10}).then(res=>{
     console.log(res)
      if (res.code == 0) {
        this.setData({
          adlist:res.data.list
        })
      }
    })
  },
  getOneId() {
    let data = {
      openId: app.globalData.user_info.openId,
      phone: this.data.phone,
      city: app.globalData.city
    }
    getOneId(data).then(res => {
      if (res.code == 0) {
        app.globalData.user_info.oneId = res.data;
        this.setData({
          oneId:res.data
        })
      }
    })
  },
  getDept(){
    let info={
      cityName:app.globalData.city
    }
    getOneDept(info).then(res=>{
      if(res.code==0){
        this.setData({
          dealersInfo:res.data
        })
        app.globalData.deptsInfo=res.data
      }
    })
  },
  goodsLis(){
     let that=this;
     let data={
       oneId:app.globalData.user_info.oneId
     }
     goodsList(data).then(res=>{
       if(res.code==0){
         let list=res.data.goodsInfoList;
         app.globalData.holdGifts=res.data.holdGifts;
         list.forEach(function(item){
           let originalPrice=item.priceAtg-item.priceDeduction;
           let handPrice=(item.priceSell-item.priceAtg)-item.priceDeduction;
           let goodsDetails=item.goodsDetails.replace(/<p>/g, '').replace(/<\/p>/g, '').replace(/<br>/g,'')  //商品详情去除html富文本标签
           item.goodsDetails=goodsDetails
           item.originalPrice=originalPrice;
           item.handPrice=handPrice;
         })
         that.setData({
          goodsList:list,
          haveORno:res.data.holdGifts  // 0无，1有  礼金包
         })
       }
     })
  },
  getAssignDetail(){
    let id={
      //  app.globalData.user_info.userDeptId
      deptId:124
    }
    assignDetail(id).then(res=>{
      if(res.code==0){
        this.setData({
          giftMsg:res.data.list[0]
        })
        this.getGiftInfo()
      }else{
        wx.showToast({
          title: res.errMsg,
          icon:'none'
        })
      }
    })
  },
  //获取礼金包信息
  getGiftInfo(){
    let data={
      id:this.data.giftId
    }
    giftInfo(data).then(res=>{
      console.log(res)
      if(res.code==0){
        this.setData({
          giftIdMsg:res.data
        })
      }
    })
  },
  hideOverlay(){
    this.setData({
      show:false
    })
  },
  cityData() {
    getCitysList().then(res => {
     // console.log(res)
      if (res.code == 0) {
        if(app.globalData.city == ''){
          app.globalData.city =  res.data[0].city[0].nameCity || ''
          this.setData({
            currentCity: res.data[0].city[0].nameCity || ''
          })
        }
        this.goodsLis();
        this.getDept()
      }else{
        wx.showToast({
          title: res.errMsg,
          icon:'none'
        })
      }
    })
  },
  chooseCity: function () {
    wx.navigateTo({
      url: '/pages/main/chooseACity/index',
    })
  },
  swiperChange(e) {
    //  console.log(e)
      let { current } = e.detail;
      this.setData({
        currentIndex: current
      })
    },  
  //商品详情
  goDetail(e) {
    let { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/goodsOrder/goodsDetail/index?goodsId=' + id
    })
  },
  get_openId() {
    let that=this;
    wx.login({
      success(res) {
        if (res.code) {
          let obj = {}
          obj['jsCode'] = res.code
          getOpenId(obj).then(res_openId => {
          console.log(res_openId,"res_openIdres_openId")
            if (res_openId.code == 0) {
              app.globalData.user_info.openId = res_openId.data.openid
              app.globalData.session_key = res_openId.data.session_key
              let obj = {}
              obj['openId'] = app.globalData.user_info.openId
              getWxUser(obj).then(res_user => {
                console.log(res_user,"res_userres_userres_user")
                if (res_user.data) {
                  app.globalData.user_info.avatarUrl = res_user.data.avatarUrl
                  app.globalData.user_info.nickName = res_user.data.nickName
                  app.globalData.user_info.openId = res_user.data.openId
                  app.globalData.user_info.phone = res_user.data.phone
                }
              });
              getOneId(obj).then(res => {
                console.log(res,"sdfsdf")
                that.setData({
                  oneId:res.data
                })
                if (res.code == 0) {
                  app.globalData.user_info.oneId = res.data
                }
                console.log(app.globalData.user_info.oneId)
              })
            }
          })
        }
      }
    })
  },
  get_gift() {
    let obj = {}
    obj['type'] = 'index'
    obj['city'] = app.globalData.city
    getSmallProgramActivities(obj).then(res => {
      if (res.code == 0) {
      //  console.log(res.data)
        let arr = res.data
        for (let i = 0; i < arr.length; i++) {
          arr[i]['startTime'] = arr[i]['startTime'].substr(0, 10)
          arr[i]['endTime'] = arr[i]['startTime'].substr(0, 10)
          arr[i]['swiper'] = []
          if (arr[i]['operationImg1']) {
            arr[i]['swiper'].push(arr[i]['operationImg1'])
          }
          if (arr[i]['operationImg2']) {
            arr[i]['swiper'].push(arr[i]['operationImg2'])
          }
          if (arr[i]['operationImg3']) {
            arr[i]['swiper'].push(arr[i]['operationImg3'])
          }
        }
        this.setData({ list_gift: arr })
      }
    })
  },
  // 去活动详情
  to_show(e) {   
    console.log( e.currentTarget.dataset['id'])
    wx.navigateTo({
      url: '/pages/gift/show/index?id=' + e.currentTarget.dataset['id']+'&activityName='+ e.currentTarget.dataset['name']
    })
  }
})